<template>
  <div>
    <h1>{{ restaurant.name }}</h1>
    <p><strong>地址:</strong> {{ restaurant.address }}</p>
    <p><strong>评分:</strong> {{ restaurant.rating }}</p>
    <p><strong>评论:</strong> {{ restaurant.comments }}</p>
    <div v-if="restaurant.image" class="image-container">
      <img :src="getImageUrl(restaurant.image)" alt="Restaurant Image" />
    </div>
    <el-button type="primary" @click="backToList">返回列表</el-button>
  </div>
</template>

<script>
export default {
  name: 'RestaurantDetail',
  data() {
    return {
      restaurant: {}
    };
  },
  created() {
    this.fetchRestaurant();
  },
  methods: {
    fetchRestaurant() {
      const id = parseInt(this.$route.params.id, 10);
      const restaurant = this.getRestaurantById(id);
      if (restaurant) {
        this.restaurant = restaurant;
      } else {
        this.$router.push({name: 'RestaurantList'});
      }
    },
    getRestaurantById(id) {
      // 假设从后端获取数据
      const restaurants = [
        {
          id: 1,
          name: '高档酒店餐厅',
          address: '芜湖市弋江区',
          rating: 4.5,
          comments: '很好吃',
          image: 'restaurant1.png'
        },
        {
          id: 2,
          name: '中档酒店餐厅',
          address: '芜湖市鸠江区',
          rating: 4.0,
          comments: '还可以',
          image: 'restaurant2.png'
        },
        {id: 3, name: '廊棚餐厅', address: '芜湖古镇', rating: 3.5, comments: '一般般', image: 'restaurant3.png'},
        {id: 4, name: '食堂', address: '安徽师范大学', rating: 4.8, comments: '太棒了', image: 'restaurant4.png'}
      ];
      return restaurants.find(r => r.id === id);
    },
    backToList() {
      this.$router.push({name: 'RestaurantList'});
    },
    getImageUrl(imagePath) {
      return new URL(`/src/assets/${imagePath}`, import.meta.url).href;
    }
  }
};
</script>

<style scoped>
h1 {
  font-size: 24px;
  color: #333;
}

p {
  font-size: 18px;
  color: #666;
  margin-bottom: 10px;
}

.el-button {
  font-size: 16px;
  margin-top: 20px;
}

.image-container {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.image-container img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
</style>